//
//  OneViewController.m
//  DLSlideController
//
//  Created by Dongle Su on 14-12-6.
//  Copyright (c) 2014年 dongle. All rights reserved.
//

#import "JianJieViewController.h"

#define CellFont      [UIFont systemFontOfSize:12]

#define cellTextColor [UIColor colorWithRed:0.47 green:0.48 blue:0.48 alpha:1.00]


@interface JianJieViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

{

    CompanyDetailBaseinfoModel * baseinfomodel;//公司信息model
    
    
    
    NSMutableArray * array;
}

@end

@implementation JianJieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
//    
//    view.backgroundColor = [UIColor redColor];
//    
//    [self.view addSubview:view];
    
    
   
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    self.view.backgroundColor = [UIColor whiteColor];
    _JianJieTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, width, height - 190 ) style:UITableViewStyleGrouped];
    
    
    _JianJieTableView.delegate = self;
    _JianJieTableView.dataSource = self;
    
    _JianJieTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, 0.01f)];
    array = [[NSMutableArray alloc]init];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(caledarNoti:) name:@"caledarNoti" object:nil];
    [self requestDataWithdate:@""];
    
    [self.view addSubview:_JianJieTableView];
    
    

    
    
    
    
    
    
    NSLog(@"one didload");
}

-(void)caledarNoti:(NSNotification *)noti
{
    NSLog(@"相册的通知接收到的消息%@",noti.object);
   
    NSArray * arr = noti.object;
    NSMutableArray * mArr = [[NSMutableArray alloc]init];
    [mArr addObject:arr.firstObject];
    if ([[[arr objectAtIndex:1] stringValue] length]  == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:1]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:1]];
    }
    
    if ([[arr.lastObject stringValue] length] == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:2]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:2]];
    }
    
    NSString * timeStr = [mArr componentsJoinedByString:@""];
    NSLog(@"timeStr is %@",timeStr);
    [self requestDataWithdate:timeStr];
}

-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        //        app.window.rootViewController = nvc
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}



-(void)requestDataWithdate:(NSString *)date
{
  
    if ([self isLogin]) {
        
        [U76HONet companyHomePageWithAction:@"" code:_yqCode ucode:[Singleton share].code dates:date andDataBlock:^(CompanyDetailModel *model) {
            NSLog(@"公司首页获取数据成功了");
            
            baseinfomodel = model.baseinfomodel;
            
            [self.JianJieTableView reloadData];
        }];
        
    }
    
    

    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

       CGFloat height = [baseinfomodel.intro heightOfSize:17 width:SCREEN_WIDTH-20];
    
    return height+80;

}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [self creatFooter];
}

-(UIView *)creatFooter{

    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    
    view.backgroundColor = [UIColor whiteColor];
    
    //////////////////////
    UIView * TopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    
    TopView.backgroundColor = [UIColor whiteColor];
    
    UIView * topBackView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 15)];
    [TopView addSubview:topBackView];
    
    topBackView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
   
    
    [view addSubview:TopView];
    
    
    UIImageView * leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 26,15 , 18)];
    
    leftImageView.image = [UIImage imageNamed:@"00d"];
    
    //leftImageView.backgroundColor =  [UIColor redColor];
    

    
        [TopView addSubview:leftImageView];

    
    
    
    UILabel * jianjieLabel =[[ UILabel alloc]initWithFrame:CGRectMake(40, 25, 200, 20)];
    
    jianjieLabel.text = @"公司简介";
    jianjieLabel.textColor = [UIColor colorWithRed:0.68 green:0.79 blue:0.34 alpha:1.00];
    
    jianjieLabel.font = [UIFont systemFontOfSize:16];
    
    
    [TopView addSubview:jianjieLabel];
    
    
    UIImageView * BottomXianView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 1)];
    BottomXianView.backgroundColor =  [UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
    
    [TopView addSubview:BottomXianView];
    
    
    CGFloat height = [baseinfomodel.intro heightOfSize:15 width:SCREEN_WIDTH-40];
    UILabel * NeiRongLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,60, SCREEN_WIDTH - 40, height)];
    NeiRongLabel.numberOfLines = 0;
    
    NeiRongLabel.font = [UIFont systemFontOfSize:15];
    
    NeiRongLabel.text =baseinfomodel.intro;
    
    NeiRongLabel.textColor = cellTextColor;
    
    [view addSubview:NeiRongLabel];



    return view;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (![baseinfomodel.Website isEqualToString:@""]) {
        return 4;
    }
    
    return 3;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 63/2;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


static NSString * string = @"sss";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        
        
    }
    

    
    if (![baseinfomodel.Website isEqualToString:@""]) {
        
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = baseinfomodel.companyname;
                break;
                
            case 1:
                cell.textLabel.text = baseinfomodel.address;
                cell.textLabel.font = CellFont;
                
                cell.textLabel.textColor = cellTextColor;
                break;

            case 2:
                cell.textLabel.text = baseinfomodel.Website;
                cell.textLabel.font = CellFont;
                cell.textLabel.textColor = cellTextColor;


                break;

            case 3:
                cell.textLabel.text = [NSString stringWithFormat:@"联系电话：%@",baseinfomodel.tel];
                cell.textLabel.font = CellFont;
                cell.textLabel.textColor = cellTextColor;


                break;

                
            default:
                break;
        }
        
        
       
        
    }
    else{
    
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = baseinfomodel.companyname;
                break;
                
            case 1:
                cell.textLabel.text = baseinfomodel.address;
                cell.textLabel.font = CellFont;
                
                cell.textLabel.textColor = cellTextColor;
                break;
                
            case 2:
                cell.textLabel.text = [NSString stringWithFormat:@"联系电话：%@",baseinfomodel.tel];
                cell.textLabel.font = CellFont;
                cell.textLabel.textColor = cellTextColor;
                
                
                break;
                
                
            default:
                break;
        }
        

    
    
    }
    
    
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    NSLog(@"%ld",(long)indexPath.row);
    
    
    
    if (![baseinfomodel.Website isEqualToString:@""]) {
        
        if (indexPath.row ==2) {
            
            NSString * UrlString = [NSString stringWithFormat:@"http://%@",baseinfomodel.Website];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UrlString]];
            
            
            
        }
        else  if (indexPath.row ==3) {
            
            
                
            
            if (baseinfomodel.tel) {
                
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",baseinfomodel.tel]]];
                
            }
           
            
        }
        

        
    }
    else{
    
        if (indexPath.row ==2) {
            
            if (baseinfomodel.tel) {
                
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",baseinfomodel.tel]]];
                
            }
            
        }
       
            
        
        

    
    
    }
    
    
}


//- (void)viewWillAppear:(BOOL)animated{
//    NSLog(@"one will appear");
//}
//- (void)viewDidAppear:(BOOL)animated{
//    NSLog(@"one appeared");
//}
//- (void)viewWillDisappear:(BOOL)animated{
//    NSLog(@"one will disappear");
//}
//- (void)viewDidDisappear:(BOOL)animated{
//    NSLog(@"one did disappear");
//}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    NSLog(@"one didReceiveMemoryWarning");
}
- (void)dealloc{
    NSLog(@"one dealloc");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
